package com.livegoods.service.impl;

import com.livegoods.commons.pojo.LivegoodsResult;
import com.livegoods.pojo.House;
import com.livegoods.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Value("${livegoods.fastdfs.nginx}")
    private String nginx;


    @Cacheable(cacheNames = "com.livegoods",key = "'detail::'+#id")
    public House findById(String id) {
        System.out.println("id = " + id);
        House house = mongoTemplate.findById(id, House.class);
        List<String> imgs = house.getImgs();
        for (int i = 0; i < imgs.size(); i++) {
            imgs.set(i,nginx+imgs.get(i));
        }
        house.setImgs(imgs);

        return house;
    }

    @Override
    public LivegoodsResult findBuytimeById(String id) {
        try {
            House house = mongoTemplate.findById(id, House.class);

            LivegoodsResult ok = LivegoodsResult.ok();
            ok.setTime(house.getBuytime().getTime());
            return ok;
        } catch (Exception e) {
            return LivegoodsResult.error("查询预定时间失败!");
        }
    }
}